package com.example.edianzu.somur.module.mine.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.edianzu.somur.R;
import com.example.edianzu.somur.utils.view.BaseActivity;
import com.example.edianzu.somur.utils.content.StatusBarUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportActivity extends BaseActivity {

    @BindView(R.id.activity_repotr_webView)
    WebView activityRepotrWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setWindowStatusBarColor(this,R.color.title);
        setContentView(R.layout.activity_dnareport);
        ButterKnife.bind(this);

        WebSettings webSetting = activityRepotrWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);   //与js交互
        webSetting.setDomStorageEnabled(true);    //开启DOM形式存储
        webSetting.setDatabaseEnabled(true);   //开启数据库形式存储
        String appCacheDir = getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();   //缓存数据的存储地址
        webSetting.setAppCachePath(appCacheDir);
        webSetting.setAppCacheEnabled(true);  //开启缓存功能
        webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);      //缓存模式
        webSetting.setAllowFileAccess(true);
        activityRepotrWebView.loadUrl(getIntent().getStringExtra("url"));
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        activityRepotrWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0){
            if(activityRepotrWebView.canGoBack()){
                activityRepotrWebView.goBack();
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}
